---
title: "Cleaning ISSP"
---
Notes: modes of data collections were mixed in Denmark (Wave 3), Kenya (Waves 3 & 4), Suriname (only available in Wave 4 and used mixed modes), and US (Wave 4; no NAs for Wave 3).

# Load

```{r}
# load packages
  source("helper-packages.R")

# load issp: religion 1
  issp_1_raw <- 
    import("../raw-data/y-multi-issp/religion-i/ZA2150.dta")

# load issp: religion 2
  issp_2_raw <- 
    import("../raw-data/y-multi-issp/religion-ii/ZA3190.dta")
  
# load issp: religion 3
  issp_3_raw <- 
    read_dta("../raw-data/y-multi-issp/religion-iii/ZA4950_v2-3-0.dta")
  
# load issp: religion 3 extra countries (indonesia, kenya, sri lanka, tanzania)
  issp_3_extras_raw <- 
    read_dta("../raw-data/y-multi-issp/religion-iii-extras/ZA5690_v1-0-1.dta")
  
# load issp: religion 4
  issp_4_raw <- 
    read_dta("../raw-data/y-multi-issp/religion-iv/ZA7570_v2-1-0.dta", encoding = "latin1") 
  
# load issp: religion 3 extra countries (indonesia, kenya, sri lanka, tanzania)
  issp_4_extras_raw <- 
    read_dta("../raw-data/y-multi-issp/religion-iv-extras/ZA7630_v1-0-0.dta")  
```

# Clean Round 1

-NOT PROCESSED:
--REASONS:
---No questions about religious intolerance

# Clean Round 2
-NOT PROCESSED:
--REASONS:
---No questions about religious intolerance

# Clean Round 3 extra countries (indonesia, kenya, sri lanka, tanzania)

```{r}
# declare dates
  issp3_extras_dates <- 
    tribble(
      ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
      "Indonesia", "10 Mar 08", "04 Apr 08",              
      "Kenya", "10 Sep 08", "01 Oct 08",  
      "Sri Lanka", "24 Sep 08", "15 Oct 08",  
      "Tanzania", "28 Aug 08", "08 Sep 08") %>% 
    mutate(
      resp_interview_start_date = as.Date(resp_interview_start_date, "%d %b %y"),
      resp_interview_end_date = as.Date(resp_interview_end_date, "%d %b %y"))

# clean
  clean_issp3_extras <- 
    issp_3_extras_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "International Social Survey Programme",
        
      # round number (character vector, title case)  
        resp_round = "Religion III",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3PQFsbu",

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode =
          case_when(
            MODE %in% c(10:25) ~ "in-person",
            TRUE ~ NA_character_),

      # country (character vector; list of countries as written in original source)
        resp_country_original = as.character(V5),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(countrycode(V5, origin = "iso3n", destination = "country.name")),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = NA) %>% 
        left_join(
          issp3_extras_dates, by = "resp_country_common") %>% 
        mutate(
          
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
     # respondent's religion (character vector that corresponds to master list)
        resp_religion =
          case_when(
            RELIGGRP %in% c(2:4, 9) ~ "Christian",
            RELIGGRP == 5 ~ "Jewish",
            RELIGGRP == 6 ~ "Muslim",
            RELIGGRP == 7 ~ "Buddhist",
            RELIGGRP == 8 ~ "Hindu",
            RELIGGRP %in% c(10:11) ~ "Other religion",
            TRUE ~ NA_character_),
    
     # respondent's religion (character vector that corresponds to master list)
        resp_denomination = 
          dplyr::recode(
            as.character(RELIGGRP),
              "1" = NA_character_,
              "2" = "Roman Catholic",
              "3" = "Protestant",
              "4" = "Christian Orthodox",
              "5" = "Jewish",
              "6" = "Islam",
              "7" = "Buddhism",
              "8"= "Hinduism",
              "9" = "Other Christian Religions",
              "10" = "Other Eastern Religions",
              "11" = "Other Religions",
              "97" = NA_character_,
              "98" = NA_character_,
              "99" = NA_character_),      

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age = as.character(AGE),        
      
      # respondent's education level
        resp_education_original =
          dplyr::recode(
            as.character(DEGREE),
            "0" = "0. No formal education [No education]",
            "1" = "1. Lowest formal qualification [Primary]",
            "2" = "2. Above lowest qualification [Primary]",
            "3" = "3. Higher secondary completed [Primary]",
            "4" = "4. Above higher secondary level, others [Primary]",
            "5" = "5. University degree completed [College]",
            .default = NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            SEX == 1 ~ 0,
            SEX == 2 ~ 1,
            TRUE ~ NA_real_),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            URBRURAL %in% c(4:5) ~ 1,
            URBRURAL %in% c(1:3) ~ 0,
            TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q14a [V29]; QTEXT: People have different religions and different religious views. Would you accept a person from a different religion or with a different religious view from yours marrying a relative of yours?; ROPTIONS: 1 = Definitely accept [=0] + 2 = Probably accept [=0] + 3 = Probably not accept [=1] + 4 = Definitely not accept [=1]; TARGET: Different religion; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(V29),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            V29 %in% c(1:2) ~ 0,
            V29 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q8a [V83]; QTEXT: What is your personal attitude towards members of the following religious groups? Christians.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(V83),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            V83 %in% c(1:3) ~ 0,
            V83 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q8b [V84]; QTEXT: What is your personal attitude towards members of the following religious groups? Muslims.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(V84),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            V84 %in% c(1:3) ~ 0,
            V84 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 4 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_4_qinfo = "NUM: Q8c [V85]; QTEXT: What is your personal attitude towards members of the following religious groups? Hindus.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Hindu; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_4_original = 
          dplyr::recode(
            as.character(V85),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_4_bin_recode = 
          case_when(
            V85 %in% c(1:3) ~ 0,
            V85 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 5 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_5_qinfo = "NUM: Q8d [V86]; QTEXT: What is your personal attitude towards members of the following religious groups? Buddhists.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Buddhist; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_5_original = 
          dplyr::recode(
            as.character(V86),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_5_bin_recode = 
          case_when(
            V86 %in% c(1:3) ~ 0,
            V86 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 6 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_6_qinfo = "NUM: Q8e [V87]; QTEXT: What is your personal attitude towards members of the following religious groups? Jews.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_6_original = 
          dplyr::recode(
            as.character(V87),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_6_bin_recode = 
          case_when(
            V87 %in% c(1:3) ~ 0,
            V87 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = "NUM: Q7 [V13]; QTEXT: Generally speaking, would you say that people can be trusted or that you can't be too careful in dealing with people?; ROPTIONS: 1 = People can almost always be trusted [=0] + 2 = People can usually be trusted [=0] + 3 = You usually can't be too careful in dealing with people [=1] + 4 = You almost always can't be too careful in dealing with people [=1]",

      # original response (as character vector)
        resp_gentrust_original = 
          dplyr::recode(
            as.character(V13),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = 
          case_when(
            V13 %in% c(1:2) ~ 0,
            V13 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q31 [V63]; QTEXT: Would you describe yourself as...; ROPTIONS: 1 = Extremely religious + 2 = Very religious + 3 = Somewhat religious + 4 = Neither religious nor non-religious + 5 = Somewhat non-religious + 6 = Very non-religious + 7 = Extremely non-religious",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(V63),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (7 - resp_religiosity_original)/6

    ) %>% 
    select(starts_with("resp_"))
```

# Clean Round 3

```{r}
# declare dates
  issp3_dates <- 
    tribble(
      ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
      "Australia", "02 Dec 09", "28 Feb 10",             
      "Austria", "22 Oct 08", "08 Dec 08", 
      "Belgium", "10 Mar 08", "10 Jul 08",
      "Chile", "07 Jun 08", "26 Jun 08",
      "Taiwan", "15 Jul 09", "06 Sep 09",
      "Croatia", "01 Jun 09", "15 Sep 09",
      "Cyprus", "07 Jul 08", "19 Sep 08",
      "Czechia", "15 Sep 08", "29 Sep 08",
      "Denmark", "22 Jan 09", "12 Jul 09",
      "Dominican Republic", "12 Dec 08", "30 Dec 08",
      "Finland", "19 Sep 08", "08 Jan 09",
      "France", "01 Sep 08", "01 Dec 08",
      "Germany", "01 Mar 08", "23 Aug 08",
      "Hungary", "30 Nov 08", "10 Dec 08",
      "Ireland", "12 Aug 08", "31 Oct 08",
      "Israel", "15 Sep 09", "15 Feb 10",
      "Italy", "03 Nov 08", "07 Mar 10",
      "Japan", "22 Nov 08", "30 Nov 08",
      "South Korea", "29 Jun 08", "30 Aug 08",
      "Latvia", "12 Jun 09", "06 Jul 09",
      "Mexico", "18 Jul 09", "03 Aug 09",
      "Netherlands", "01 Mar 08", "01 Dec 08",
      "New Zealand", "26 Aug 08", "30 Nov 08",
      "Norway", "07 May 08", "09 Jul 08",
      "Philippines", "30 Nov 07", "12 Apr 08", # this will get replaced with religion-specific date ranges below (where religion is available)
      "Poland", "02 Jun 10", "09 Jul 10",
      "Portugal", "01 Jun 09", "01 Nov 09",
      "Russia", "02 Jan 09", "26 Jan 09",
      "Slovakia", "07 Oct 08", "28 Oct 08",
      "Slovenia", "24 Mar 09", "04 Jun 09",
      "South Africa", "03 Nov 08", "20 Dec 08",
      "Spain", "21 Oct 08", "05 Dec 08",
      "Sweden", "19 Feb 08", "29 Apr 08",
      "Switzerland", "08 Feb 08", "05 Sep 09",
      "Turkey", "01 Dec 08", "21 Feb 09",
      "Ukraine", "11 Oct 08", "20 Oct 08",
      "US", "16 Apr 08", "15 Sep 08",
      "Uruguay", "20 Sep 08", "20 Dec 08",
      "Venezuela", "04 Jul 08", "12 Jul 08") %>% 
    mutate(
      resp_interview_start_date = as.Date(resp_interview_start_date, "%d %b %y"),
      resp_interview_end_date = as.Date(resp_interview_end_date, "%d %b %y"),
      resp_country_common = countryname(resp_country_common))

# clean
  clean_issp3 <- 
    issp_3_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "International Social Survey Programme",
        
      # round number (character vector, title case)  
        resp_round = "Religion III",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/38P0CGl",

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode =
          case_when(
            MODE %in% c(10:24) ~ "in-person",
            MODE %in% c(30:40) ~ "mail",
            MODE %in% c(41) ~ "internet",
            MODE %in% c(52) ~ "phone",
            TRUE ~ NA_character_),

      # country (character vector; list of countries as written in original source)
        resp_country_original = as.character(V5),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(countrycode(V5, origin = "iso3n", destination = "country.name")),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = NA) %>% 
        left_join(
          issp3_dates, by = "resp_country_common") %>% 
        mutate(
          resp_interview_start_date = 
            case_when(
              C_ALPHAN == "GB-GBN" ~ as.Date("2008-06-10"),
              C_ALPHAN == "GB-NIR" ~ as.Date("2008-10-01"),
              C_ALPHAN == "PH" & RELIG %in% c(100, 200, 401, 409) ~ as.Date("2007-11-30"),
              C_ALPHAN == "PH" & RELIG %in% c(600) ~ as.Date("2008-04-02"),
              TRUE ~ resp_interview_start_date),
          resp_interview_end_date = 
            case_when(
              C_ALPHAN == "GB-GBN" ~ as.Date("2008-11-11"),
              C_ALPHAN == "GB-NIR" ~ as.Date("2009-02-27"),
              C_ALPHAN == "PH" & RELIG %in% c(100, 200, 401, 409) ~ as.Date("2007-12-03"),
              C_ALPHAN == "PH" & RELIG %in% c(600) ~ as.Date("2008-04-12"), 
              TRUE ~ resp_interview_end_date), # note that we lack dates for respondents who did not declare a religion [the fieldwork dates were divided by christian/"moslem"]
    
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
     # respondent's religion (character vector that corresponds to master list)
        resp_religion =
          case_when(
            RELIGGRP %in% c(2:4, 9) ~ "Christian",
            RELIGGRP == 5 ~ "Jewish",
            RELIGGRP == 6 ~ "Muslim",
            RELIGGRP == 7 ~ "Buddhist",
            RELIGGRP == 8 ~ "Hindu",
            RELIGGRP %in% c(10:11) ~ "Other religion",
            TRUE ~ NA_character_),
    
     # respondent's religion (character vector that corresponds to master list)
        resp_denomination = 
          dplyr::recode(
            as.character(RELIGGRP),
              "1" = NA_character_,
              "2" = "Roman Catholic",
              "3" = "Protestant",
              "4" = "Christian Orthodox",
              "5" = "Jewish",
              "6" = "Islam",
              "7" = "Buddhism",
              "8"= "Hinduism",
              "9" = "Other Christian Religions",
              "10" = "Other Eastern Religions",
              "11" = "Other Religions",
              "97" = NA_character_,
              "98" = NA_character_,
              "99" = NA_character_),      

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          case_when(
            AGE == 89 & resp_country_common == "United States" ~ "89+",
            AGE == 98 ~ "98+",
            AGE == 99 ~ NA_character_,
            TRUE ~ as.character(AGE)),        
      
      # respondent's education level
        resp_education_original =
          dplyr::recode(
            as.character(DEGREE),
            "0" = "0. No formal education [No education]",
            "1" = "1. Lowest formal qualification [Primary]",
            "2" = "2. Above lowest qualification (qualifications which are above the lowest qualification, but below the usual entry requirement for universities (intermediary secondary completed)) [Primary]",
            "3" = "3. Higher secondary completed ((usual) entry requirement for universities (the German Abitur, the French Bac, English A-level, etc.)) [Primary]",
            "4" = "4. Above higher secondary level, others (qualifications which are above the higher secondary level, but below a full university degree) [Primary]",
            "5" = "5. University degree completed, graduate studies [College]",
            .default = NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            SEX == 1 ~ 0,
            SEX == 2 ~ 1,
            TRUE ~ NA_real_),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            URBRURAL %in% c(4:5) ~ 1,
            URBRURAL %in% c(1:3) ~ 0,
            TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q14a [V29]; QTEXT: People have different religions and different religious views. Would you accept a person from a different religion or with a different religious view from yours marrying a relative of yours?; ROPTIONS: 1 = Definitely accept [=0] + 2 = Probably accept [=0] + 3 = Probably not accept [=1] + 4 = Definitely not accept [=1]; TARGET: Different religion; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(V29),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            V29 %in% c(1:2) ~ 0,
            V29 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q8a [V83]; QTEXT: What is your personal attitude towards members of the following religious groups? Christians.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(V83),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            V83 %in% c(1:3) ~ 0,
            V83 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q8b [V84]; QTEXT: What is your personal attitude towards members of the following religious groups? Muslims.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(V84),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            V84 %in% c(1:3) ~ 0,
            V84 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 4 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_4_qinfo = "NUM: Q8c [V85]; QTEXT: What is your personal attitude towards members of the following religious groups? Hindus.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Hindu; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_4_original = 
          dplyr::recode(
            as.character(V85),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_4_bin_recode = 
          case_when(
            V85 %in% c(1:3) ~ 0,
            V85 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 5 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_5_qinfo = "NUM: Q8d [V86]; QTEXT: What is your personal attitude towards members of the following religious groups? Buddhists.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Buddhist; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_5_original = 
          dplyr::recode(
            as.character(V86),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_5_bin_recode = 
          case_when(
            V86 %in% c(1:3) ~ 0,
            V86 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 6 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_6_qinfo = "NUM: Q8e [V87]; QTEXT: What is your personal attitude towards members of the following religious groups? Jews.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_6_original = 
          dplyr::recode(
            as.character(V87),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_6_bin_recode = 
          case_when(
            V87 %in% c(1:3) ~ 0,
            V87 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = "NUM: Q7 [V13]; QTEXT: Generally speaking, would you say that people can be trusted or that you can't be too careful in dealing with people?; ROPTIONS: 1 = People can almost always be trusted [=0] + 2 = People can usually be trusted [=0] + 3 = You usually can't be too careful in dealing with people [=1] + 4 = You almost always can't be too careful in dealing with people [=1]",

      # original response (as character vector)
        resp_gentrust_original = 
          dplyr::recode(
            as.character(V13),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = 
          case_when(
            V13 %in% c(1:2) ~ 0,
            V13 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q31 [V63]; QTEXT: Would you describe yourself as...; ROPTIONS: 1 = Extremely religious + 2 = Very religious + 3 = Somewhat religious + 4 = Neither religious nor non-religious + 5 = Somewhat non-religious + 6 = Very non-religious + 7 = Extremely non-religious",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(V63),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (7 - resp_religiosity_original)/6

    ) %>% 
    select(starts_with("resp_"))
```

# Clean Round 4

```{r}
# declare extra dates to deal with some missings
  issp4_main_dates <- 
    tribble(
      ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
      "Austria", "03 Apr 18", "15 Jun 18",
      "France", "15 Feb 18", "19 Nov 18",
      "Japan", "27 Oct 18", "04 Nov 18",
      "New Zealand", "07 Sep 18", "30 Apr 19",
      "Slovenia", "25 Mar 19", "16 Jun 19") %>% 
    mutate(
      resp_interview_start_date = as.Date(resp_interview_start_date, "%d %b %y"),
      resp_interview_end_date = as.Date(resp_interview_end_date, "%d %b %y"))

# clean
  clean_issp4 <- 
    issp_4_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "International Social Survey Programme",
        
      # round number (character vector, title case)  
        resp_round = "Religion IV",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3GtwkVS",

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode =
          case_when(
            MODE %in% c(10:14) ~ "in-person",
            MODE %in% c(20:24) ~ "internet",
            MODE %in% c(30:31) ~ "in-person",
            MODE %in% c(32:34) ~ "mail",
            MODE %in% c(40:60) ~ "internet",
            TRUE ~ NA_character_),
    
      # country (character vector; list of countries as written in original source)
        resp_country_original = 
          dplyr::recode(
            as.character(country),
            "40" = "AT-Austria",
            "100" = "BG-Bulgaria",
            "152" = "CL-Chile",
            "158" = "TW-Taiwan",
            "191" = "HR-Croatia",
            "203" = "CZ-Czech Republic",
            "208" = "DK-Denmark",
            "246" = "FI-Finland",
            "250" = "FR-France",
            "268" = "GE-Georgia",
            "276"= "DE-Germany",
            "348" = "HU-Hungary",
            "352" = "IS-Iceland",
            "376" = "IL-Israel",
            "380" = "IT-Italy",
            "392" = "JP-Japan",
            "410" = "KR-Korea (South)",
            "440" = "LT-Lithuania",
            "554" = "NZ-New Zealand",
            "578" = "NO-Norway",
            "608" = "PH-Philippines",
            "643" = "RU-Russia",
            "703" = "SK-Slovak Republic",
            "705" = "SI-Slovenia",
            "710" = "ZA-South Africa",
            "724" = "ES-Spain",
            "740" = "SR-Suriname",
            "752" = "SE-Sweden",
            "756" = "CH-Switzerland",
            "764" = "TH-Thailand",
            "792" = "TR-Turkey",
            "826" = "GB-Great Britain and/or United Kingdom",
            "840" = "US-United States"),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date =
          make_date(year = DATEYR,
                    month = DATEMO,
                    day = DATEDY)) %>% 
      left_join(
        issp4_main_dates, by = "resp_country_common") %>% 
      mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
     # respondent's religion (character vector that corresponds to master list)
        resp_religion =
          case_when(
            RELIGGRP %in% c(1:4) ~ "Christian",
            RELIGGRP == 5 ~ "Jewish",
            RELIGGRP == 6 ~ "Muslim",
            RELIGGRP == 7 ~ "Buddhist",
            RELIGGRP == 8 ~ "Hindu",
            RELIGGRP %in% c(9:10) ~ "Other religion",
            TRUE ~ NA_character_),
    
     # respondent's religion (character vector that corresponds to master list)
        resp_denomination = 
          dplyr::recode(
            as.character(RELIGGRP),
            "0" = NA_character_,
            "1" = "Catholic",
            "2" = "Protestant",
            "3" = "Orthodox",
            "4" = "Other Christian",
            "5" = "Jewish",
            "6" = "Muslim",
            "7" = "Buddhist",
            "8" = "Hindu",
            "9" = "Other Asian Religions",
            "10" = "Other Religions",
            "97" = NA_character_,
            "98" = NA_character_,
            "99" = NA_character_),      

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          case_when(
            AGE == 15 ~ "15",
            AGE == 105 ~ "89+",
            AGE == 999 ~ NA_character_,
            country == 840 & AGE == 89 ~ "89+",
            TRUE ~ as.character(AGE)),        
      
      # respondent's education level
        resp_education_original =
          dplyr::recode(
            as.character(DEGREE),
            "0" = "0. No formal education [No education]",
            "1" = "1. Primary school (elementary education) [Primary]",
            "2" = "2. Lower secondary (secondary completed that does not allow entry to university: end of obligatory school) [Primary]",
            "3" = "3. Upper secondary (programs that allow entry to university) [Primary]",
            "4" = "4. Post secondary, non-tertiary (other upper secondary programs toward the labour market or technical formation) [Primary]",
            "5" = "5. Lower level tertiary, first stage (also technical schools at a tertiary level) [College]",
            "6" = "6. Upper level tertiary (Master, Doctor) [College]",
            .default = NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            SEX == 1 ~ 0,
            SEX == 2 ~ 1,
            TRUE ~ NA_real_),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            URBRURAL %in% c(4:5) ~ 1,
            URBRURAL %in% c(1:3) ~ 0,
            TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q11 [v17]; QTEXT: People have different religions and different religious views. Would you accept a person from a different religion or with a very different religious view from yours marrying a relative of yours?; ROPTIONS: 1 = Definitely accept [=0] + 2 = Probably accept [=0] + 3 = Probably not accept [=1] + 4 = Definitely not accept [=1]; TARGET: Different religion; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(v17),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            v17 %in% c(1:2) ~ 0,
            v17 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q35a [v57]; QTEXT: What is your personal attitude towards members of the following religious groups? Christians.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(v57),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            v57 %in% c(1:3) ~ 0,
            v57 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q35b [v58]; QTEXT: What is your personal attitude towards members of the following religious groups? Muslims.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(v58),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            v58 %in% c(1:3) ~ 0,
            v58 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 4 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_4_qinfo = "NUM: Q35c [v59]; QTEXT: What is your personal attitude towards members of the following religious groups? Hindus.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Hindu; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_4_original = 
          dplyr::recode(
            as.character(v59),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_4_bin_recode = 
          case_when(
            v59 %in% c(1:3) ~ 0,
            v59 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 5 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_5_qinfo = "NUM: Q35d [v60]; QTEXT: What is your personal attitude towards members of the following religious groups? Buddhists.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Buddhist; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_5_original = 
          dplyr::recode(
            as.character(v60),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_5_bin_recode = 
          case_when(
            v60 %in% c(1:3) ~ 0,
            v60 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 6 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_6_qinfo = "NUM: Q35e [v61]; QTEXT: What is your personal attitude towards members of the following religious groups? Jews.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_6_original = 
          dplyr::recode(
            as.character(v61),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_6_bin_recode = 
          case_when(
            v61 %in% c(1:3) ~ 0,
            v61 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 7 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_7_qinfo = "NUM: O6a [v72]; QTEXT: Do you consider those belonging to the following groups as threatening or not threatening? Christians.; ROPTIONS: 1 = Very threatening [=1] + 2 = Somewhat threatening [=1] + 3 = Not very threatening [=0] + 4 = Not at all threatening [=0]; TARGET: Christian; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_7_original = 
          dplyr::recode(
            as.character(v72),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_7_bin_recode = 
          case_when(
            v72 %in% c(1:2) ~ 1,
            v72 %in% c(3:4) ~ 0,
            TRUE ~ NA_real_),    
    
    #########################  
    ### SOCIAL DISTANCE 8 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_8_qinfo = "NUM: O6b [v73]; QTEXT: Do you consider those belonging to the following groups as threatening or not threatening? Muslims.; ROPTIONS: 1 = Very threatening [=1] + 2 = Somewhat threatening [=1] + 3 = Not very threatening [=0] + 4 = Not at all threatening [=0]; TARGET: Muslim; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_8_original = 
          dplyr::recode(
            as.character(v73),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_8_bin_recode = 
          case_when(
            v73 %in% c(1:2) ~ 1,
            v73 %in% c(3:4) ~ 0,
            TRUE ~ NA_real_),   
    
    #########################  
    ### SOCIAL DISTANCE 9 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_9_qinfo = "NUM: O6c [v74]; QTEXT: Do you consider those belonging to the following groups as threatening or not threatening? Hindus.; ROPTIONS: 1 = Very threatening [=1] + 2 = Somewhat threatening [=1] + 3 = Not very threatening [=0] + 4 = Not at all threatening [=0]; TARGET: Hindu; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_9_original = 
          dplyr::recode(
            as.character(v74),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_9_bin_recode = 
          case_when(
            v74 %in% c(1:2) ~ 1,
            v74 %in% c(3:4) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 10 ##  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_10_qinfo = "NUM: O6d [v75]; QTEXT: Do you consider those belonging to the following groups as threatening or not threatening? Buddhists.; ROPTIONS: 1 = Very threatening [=1] + 2 = Somewhat threatening [=1] + 3 = Not very threatening [=0] + 4 = Not at all threatening [=0]; TARGET: Buddhist; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_10_original = 
          dplyr::recode(
            as.character(v75),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_10_bin_recode = 
          case_when(
            v75 %in% c(1:2) ~ 1,
            v75 %in% c(3:4) ~ 0,
            TRUE ~ NA_real_), 
    
    #########################  
    ### SOCIAL DISTANCE 11 ##  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_11_qinfo = "NUM: O6e [v76]; QTEXT: Do you consider those belonging to the following groups as threatening or not threatening? Jews.; ROPTIONS: 1 = Very threatening [=1] + 2 = Somewhat threatening [=1] + 3 = Not very threatening [=0] + 4 = Not at all threatening [=0]; TARGET: Jewish; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_11_original = 
          dplyr::recode(
            as.character(v76),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_11_bin_recode = 
          case_when(
            v76 %in% c(1:2) ~ 1,
            v76 %in% c(3:4) ~ 0,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = "NUM: O7 [v78]; QTEXT: Generally speaking, would you say that people can be trusted or that you can't be too careful in dealing with people?; ROPTIONS: 1 = People can almost always be trusted [=0] + 2 = People can usually be trusted [=0] + 3 = You usually can't be too careful in dealing with people [=1] + 4 = You almost always can't be too careful in dealing with people [=1]",

      # original response (as character vector)
        resp_gentrust_original = 
          dplyr::recode(
            as.character(v78),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = 
          case_when(
            v78 %in% c(1:2) ~ 0,
            v78 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q29 [v48]; QTEXT: Would you describe yourself as...; ROPTIONS: 1 = Extremely religious + 2 = Very religious + 3 = Somewhat religious + 4 = Neither religious nor non-religious + 5 = Somewhat non-religious + 6 = Very non-religious + 7 = Extremely non-religious",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(v48),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (7 - resp_religiosity_original)/6
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean round 4 extras

```{r}
# missing interview dates (month and day) in 2 countries:
## dz (algeria): Fieldwork dates: 2018-09-10 to 2018-11-24
## mn: mongolia: Fieldwork dates: 2018.08.20 – 2018.10.17
## austria: Fieldwork dates: 03.04.2018 - 15.06.2018
# declare dates
  issp4_extras_dates <- 
    tribble(
      ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
      "Algeria", "10 Sep 2018", "24 Nov 2018", 
      "Mongolia", "20 Aug 2018", "17 Oct 2018") %>% 
    mutate(
      resp_interview_start_date = as.Date(resp_interview_start_date, "%d %b %y"),
      resp_interview_end_date = as.Date(resp_interview_end_date, "%d %b %y"))

# clean
  issp_4_extras_clean <- 
    issp_4_extras_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "International Social Survey Programme",
        
      # round number (character vector, title case)  
        resp_round = "Religion IV",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3MXf25P",

      # survey mode (in-person/phone/internet/mail)
        resp_survey_mode = "in-person", # verified in individual country documents

      # country (character vector; list of countries as written in original source)
        resp_country_original = c_alphan,

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(countrycode(resp_country_original, origin = "iso2c", destination = "country.name")),

      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date =
          make_date(year = DATEYR,
                    month = DATEMO,
                    day = DATEDY)) %>% 
        left_join(
          issp4_extras_dates, by = "resp_country_common") %>% 
        mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
     # respondent's religion (character vector that corresponds to master list)
        resp_religion =
          case_when(
            RELIGGRP %in% c(1:4) ~ "Christian",
            RELIGGRP == 5 ~ "Jewish",
            RELIGGRP == 6 ~ "Muslim",
            RELIGGRP == 7 ~ "Buddhist",
            RELIGGRP == 8 ~ "Hindu",
            RELIGGRP %in% c(9:10) ~ "Other religion",
            TRUE ~ NA_character_),

     # respondent's religion (character vector that corresponds to master list)
        resp_denomination = 
          dplyr::recode(
            as.character(RELIGGRP),
            "0" = NA_character_,
            "1" = "Catholic",
            "2" = "Protestant",
            "3" = "Orthodox",
            "4" = "Other Christian",
            "5" = "Jewish",
            "6" = "Muslim",
            "7" = "Buddhist",
            "8" = "Hindu",
            "9" = "Other Asian Religions",
            "10" = "Other Religions",
            "97" = NA_character_,
            "98" = NA_character_,
            "99" = NA_character_),      

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          case_when(
            AGE == 999 ~ NA_character_,
            TRUE ~ as.character(AGE)),        
      
      # respondent's education level
        resp_education_original =
          dplyr::recode(
            as.character(DEGREE),
            "0" = "0. No formal education [No education]",
            "1" = "1. Primary school (elementary education) [Primary]",
            "2" = "2. Lower secondary (secondary completed that does not allow entry to university: end of obligatory school) [Primary]",
            "3" = "3. Upper secondary (programs that allow entry to university) [Primary]",
            "4" = "4. Post secondary, non-tertiary (other upper secondary programs toward the labour market or technical formation) [Primary]",
            "5" = "5. Lower level tertiary, first stage (also technical schools at a tertiary level) [College]",
            "6" = "6. Upper level tertiary (Master, Doctor) [College]",
            .default = NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female =
          case_when(
            SEX == 1 ~ 0,
            SEX == 2 ~ 1,
            TRUE ~ NA_real_),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            URBRURAL %in% c(4:5) ~ 1,
            URBRURAL %in% c(1:3) ~ 0,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q11 [v17]; QTEXT: People have different religions and different religious views. Would you accept a person from a different religion or with a very different religious view from yours marrying a relative of yours?; ROPTIONS: 1 = Definitely accept [=0] + 2 = Probably accept [=0] + 3 = Probably not accept [=1] + 4 = Definitely not accept [=1]; TARGET: Different religion; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(v17),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            v17 %in% c(1:2) ~ 0,
            v17 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q35a [v57]; QTEXT: What is your personal attitude towards members of the following religious groups? Christians.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Christian; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(v57),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            v57 %in% c(1:3) ~ 0,
            v57 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q35b [v58]; QTEXT: What is your personal attitude towards members of the following religious groups? Muslims.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Muslim; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(v58),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            v58 %in% c(1:3) ~ 0,
            v58 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 4 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_4_qinfo = "NUM: Q35c [v59]; QTEXT: What is your personal attitude towards members of the following religious groups? Hindus.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Hindu; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_4_original = 
          dplyr::recode(
            as.character(v59),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_4_bin_recode = 
          case_when(
            v59 %in% c(1:3) ~ 0,
            v59 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 5 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_5_qinfo = "NUM: Q35d [v60]; QTEXT: What is your personal attitude towards members of the following religious groups? Buddhists.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Buddhist; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_5_original = 
          dplyr::recode(
            as.character(v60),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_5_bin_recode = 
          case_when(
            v60 %in% c(1:3) ~ 0,
            v60 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 6 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_6_qinfo = "NUM: Q35e [v61]; QTEXT: What is your personal attitude towards members of the following religious groups? Jews.; ROPTIONS: 1 = Very positive [=0] + 2 = Somewhat positive [=0] + 3 = Neither positive nor negative [=0] + 4 = Somewhat negative [=1] + 5 = Very negative [=1]; TARGET: Jewish; TYPE: Favorability",
      
      # original response (as character vector)
        resp_soc_dist_6_original = 
          dplyr::recode(
            as.character(v61),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_6_bin_recode = 
          case_when(
            v61 %in% c(1:3) ~ 0,
            v61 %in% c(4:5) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 7 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_7_qinfo = "NUM: O6a [v72]; QTEXT: Do you consider those belonging to the following groups as threatening or not threatening? Christians.; ROPTIONS: 1 = Very threatening [=1] + 2 = Somewhat threatening [=1] + 3 = Not very threatening [=0] + 4 = Not at all threatening [=0]; TARGET: Christian; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_7_original = 
          dplyr::recode(
            as.character(v72),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_7_bin_recode = 
          case_when(
            v72 %in% c(1:2) ~ 1,
            v72 %in% c(3:4) ~ 0,
            TRUE ~ NA_real_),    
    
    #########################  
    ### SOCIAL DISTANCE 8 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_8_qinfo = "NUM: O6b [v73]; QTEXT: Do you consider those belonging to the following groups as threatening or not threatening? Muslims.; ROPTIONS: 1 = Very threatening [=1] + 2 = Somewhat threatening [=1] + 3 = Not very threatening [=0] + 4 = Not at all threatening [=0]; TARGET: Muslim; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_8_original = 
          dplyr::recode(
            as.character(v73),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_8_bin_recode = 
          case_when(
            v73 %in% c(1:2) ~ 1,
            v73 %in% c(3:4) ~ 0,
            TRUE ~ NA_real_),   
    
    #########################  
    ### SOCIAL DISTANCE 9 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_9_qinfo = "NUM: O6c [v74]; QTEXT: Do you consider those belonging to the following groups as threatening or not threatening? Hindus.; ROPTIONS: 1 = Very threatening [=1] + 2 = Somewhat threatening [=1] + 3 = Not very threatening [=0] + 4 = Not at all threatening [=0]; TARGET: Hindu; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_9_original = 
          dplyr::recode(
            as.character(v74),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_9_bin_recode = 
          case_when(
            v74 %in% c(1:2) ~ 1,
            v74 %in% c(3:4) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 10 ##  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_10_qinfo = "NUM: O6d [v75]; QTEXT: Do you consider those belonging to the following groups as threatening or not threatening? Buddhists.; ROPTIONS: 1 = Very threatening [=1] + 2 = Somewhat threatening [=1] + 3 = Not very threatening [=0] + 4 = Not at all threatening [=0]; TARGET: Buddhist; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_10_original = 
          dplyr::recode(
            as.character(v75),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_10_bin_recode = 
          case_when(
            v75 %in% c(1:2) ~ 1,
            v75 %in% c(3:4) ~ 0,
            TRUE ~ NA_real_), 
    
    #########################  
    ### SOCIAL DISTANCE 11 ##  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_11_qinfo = "NUM: O6e [v76]; QTEXT: Do you consider those belonging to the following groups as threatening or not threatening? Jews.; ROPTIONS: 1 = Very threatening [=1] + 2 = Somewhat threatening [=1] + 3 = Not very threatening [=0] + 4 = Not at all threatening [=0]; TARGET: Jewish; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_11_original = 
          dplyr::recode(
            as.character(v76),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_11_bin_recode = 
          case_when(
            v76 %in% c(1:2) ~ 1,
            v76 %in% c(3:4) ~ 0,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = "NUM: O7 [v78]; QTEXT: Generally speaking, would you say that people can be trusted or that you can't be too careful in dealing with people?; ROPTIONS: 1 = People can almost always be trusted [=0] + 2 = People can usually be trusted [=0] + 3 = You usually can't be too careful in dealing with people [=1] + 4 = You almost always can't be too careful in dealing with people [=1]",

      # original response (as character vector)
        resp_gentrust_original = 
          dplyr::recode(
            as.character(v78),
            "0" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = 
          case_when(
            v78 %in% c(1:2) ~ 0,
            v78 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q29 [v48]; QTEXT: Would you describe yourself as...; ROPTIONS: 1 = Extremely religious + 2 = Very religious + 3 = Somewhat religious + 4 = Neither religious nor non-religious + 5 = Somewhat non-religious + 6 = Very non-religious + 7 = Extremely non-religious",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(v48),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (7 - resp_religiosity_original)/6
    
    ) %>% 
    select(starts_with("resp_"))
```

# Stack data

```{r}
# stack
  issp_stacked <-
    clean_issp3 %>% 
    bind_rows(clean_issp3_extras) %>% 
    bind_rows(clean_issp4) %>% 
    bind_rows(issp_4_extras_clean)
```

# Save data

```{r}
  saveRDS(issp_stacked, "../cleaned-data/y-6-multi-issp.rds")
```
